package com.spark.tags;

import java.util.Map;

import com.spark.core.SparkContext;
import com.spark.core.XMap;


/**
 * 文本输入框.
 * 
 * @version $Id: TextField.java v 1.0 2010-12-20 上午10:24:45 IBM Exp $
 * @author <a href="mailto:njq.niu@hand-china.com">vincent</a>
 */
public class TextField extends InputField {	
	
	public static final String DEFAULT_TEMPLATES = "textField.ftl";
	
	public static final String INPUT_TYPE = "inputType";
	public static final String DEFAULT_INPUT_TYPE = "input";
	public static final String PROPERTITY_INPUT_CASE = "inputCase";
	
	public void init(XMap view, SparkContext context) throws Exception{
		super.init(view, context);
		
		String inputCase = view.getString(PROPERTITY_INPUT_CASE, "");
		if(!"".equals(inputCase)) {
			context.addJsonConfig(PROPERTITY_INPUT_CASE, inputCase.toLowerCase());
		}
		context.put(INPUT_TYPE, DEFAULT_INPUT_TYPE);
	}
	
	
	public String getTemplate(){
		return DEFAULT_TEMPLATES;
	}
	
	
	@SuppressWarnings("unchecked")
	protected String build(SparkContext context){		
		Map map = context.getMap();
		StringBuilder sb = new StringBuilder();
		sb.append("<div class=\"").append(get(map,"className")).append("\" style=\"width:").append(get(map,"width")).append("px;height:").append(get(map,"height")).append("px;").append(get(map,"style")).append("\" id=\"").append(get(map,"id")).append("\">\r");
		sb.append("    <div><input tabindex=\"").append(get(map,"tabIndex")).append("\" type=\"").append(get(map,"inputType")).append("\" class=\"s_tf\" stype=\"input\" style=\"width:").append(get(map,"inputWidth")).append("px;line-height:").append(get(map,"lineHeight")).append("px;height:").append(get(map,"inputHeight")).append("px;").append(get(map,"inputStyle")).append("\" ").append(get(map,"readOnly")).append(" autoComplete=\"off\" value=\"").append(get(map,"value")).append("\" /></div>	\r");
		sb.append("</div>\r");
		sb.append("<script>new Spark.TextField(").append(get(map,"config")).append(");</script>\r");
		return sb.toString();
	}
}
